package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;

    //增加检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    //分页查询检查项
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //支持 ServletRequest,Map,POJO 对象，需要配合 params 参数
        PageHelper.startPage(currentPage, pageSize);
        //紧跟着的第一个select方法会被分页
        //后面的不会被分页，除非再次调用PageHelper.startPage
        /*List<User> list = userMapper.selectIf(1);*/
        List<CheckItem> checkItemList = checkItemMapper.findByCondition(queryString);
        //用PageInfo对结果进行包装
        PageInfo<CheckItem> pageInfo = new PageInfo<>(checkItemList);


        /*   //分页查询索引号
        int firstResult = (currentPage -1)*pageSize;
        //查询总记录数
        long count=checkItemMapper.findCount(queryString);
        //查询分页数据集合
        List<CheckItem> checkItemList= checkItemMapper.findByCondition(firstResult, pageSize, queryString); */
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    //删除检查项
    @Override
    public void deleteById(Integer id) {
        //通过检查项的id查询是否在中间表中被引用
        Integer count=checkItemMapper.findCountByCheckItemId(id);
        if (count>0) {
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }
        checkItemMapper.deleteById(id);
    }

    //编辑检查项
    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    //查询检查项
    @Override
    public List<CheckItem> findCheckItem() {
        List<CheckItem> checkItemList= checkItemMapper.findCheckItem();
        return checkItemList;
    }

    @Override
    public List<Integer> findCheckItemById(Integer id) {
        List<Integer> checkItemList =checkItemMapper.findCheckItemById(id);
        System.out.println("impl");
        System.out.println(checkItemList);
        System.out.println("impl"+checkItemList);
        return checkItemList;
    }
}
